package com.probiz.estore.product.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.probiz.estore.common.model.product.ProductAttGroup;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductAttGroupDao;
import com.probiz.estore.product.service.ProductAttGroupManager;


/**
 * Manager implementation for ProductAttGroup, responsible for business processing, and communicate between web and persistence layer.
 */
public class ProductAttGroupManagerImpl extends GenericManagerImpl<ProductAttGroup> implements ProductAttGroupManager {
	
	private ProductAttGroupDao productAttGroupDao = null;

	/**
	 * @param productAttGroupDao
	 *            the productAttGroupDao to set
	 */
	public void setProductAttGroupDao(ProductAttGroupDao productAttGroupDao) {
		this.productAttGroupDao = productAttGroupDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = productAttGroupDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ProductAttGroup entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ProductAttGroup entity) {

	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductAttGroupManager#saveGroupsFromArrays(com.probiz.estore.common.model.product.ProductType, java.lang.String[], java.lang.String[], java.lang.String[])
	 */
	public List<String> saveGroupsFromArrays(ProductType productType,
			String[] groupIds, String[] groupNames, String[] groupSortOrders){
		List<String> newGroupIdList = new ArrayList<String>();
		//删除group
		List<String> groupIdList=Arrays.asList(groupIds);
		Set<ProductAttGroup> formerGroupList=productType.getProductAttGroups(); //原来的属性组
		for (ProductAttGroup group : formerGroupList) {
			if(!groupIdList.contains(group.getProductAttGroupId().toString())){
				deleteById(group.getProductAttGroupId());
			}
		}
		//增加或者修改group
		for (int i = 0; i < groupIds.length; i++) {
			ProductAttGroup group;
			if(groupIds[i].equals("")){
				//case:新增
				group = new ProductAttGroup();
			}else{
				//case:修改
				group = getById(Integer.valueOf(groupIds[i]));
			}
			group.setProductAttGroupName(groupNames[i]);
			group.setSortOrder(i);  //界面上不需要用groupSortOrders
			group.setProductTypeId(productType.getProductTypeId());
			save(group);
			newGroupIdList.add(group.getProductAttGroupId().toString());
		}
		return newGroupIdList;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductAttGroupManager#getProductAttGroupsByProductTypeId(java.lang.Integer)
	 */
	public List<ProductAttGroup> getProductAttGroupsByProductTypeId(Integer productTypeId){
		return productAttGroupDao.getProductAttGroupsByProductTypeId(productTypeId);
	}

}
